package com.gdgk.shop.api.controller;

import com.gdgk.shop.bean.app.param.CloseOrderParam;
import com.gdgk.shop.bean.app.param.OutTradeNoSearchParam;
import com.gdgk.shop.bean.app.param.TransactionIdSearchParam;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.CloseOrderRequest;
import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByIdRequest;
import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByOutTradeNoRequest;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wxorder")
@AllArgsConstructor
public class PayStatusController {
    public static JsapiServiceExtension service;
    /**
     * 微信支付订单号查询订单
     */
    @PostMapping("/wxIdSearch")
    @Operation(summary = "微信支付订单号查询订单", description = "微信支付订单号查询订单")
    public Transaction queryOrderById(@RequestBody TransactionIdSearchParam transactionIdSearchParam) {

        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        request.setTransactionId(transactionIdSearchParam.getTransactionId());
        request.setMchid(transactionIdSearchParam.getMchId());
        return service.queryOrderById(request);
    }

    /** 商户订单号查询订单 */
    @PostMapping("/tradeNoSearch")
    @Operation(summary = "商户订单号查询订单", description = "商户订单号查询订单")
    public static Transaction queryOrderByOutTradeNo(OutTradeNoSearchParam outTradeNoSearchParam) {

        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setOutTradeNo(outTradeNoSearchParam.getOutTradeNo());
        request.setMchid(outTradeNoSearchParam.getMchId());
        return service.queryOrderByOutTradeNo(request);
    }

    /** 关闭订单 */
    @PostMapping("/closeOrder")
    @Operation(summary = "关闭过期订单", description = "关闭过期订单")
    public static void closeOrder(CloseOrderParam closeOrderParam) {
        CloseOrderRequest request = new CloseOrderRequest();
        request.setOutTradeNo(closeOrderParam.getOutTradeNo());
        request.setMchid(closeOrderParam.getMchId());
        service.closeOrder(request);
    }

}
